package com.example.zhangxinying.smratchatnew;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zhangxinying.smratchatnew.view.SpannableTextView;

import java.util.ArrayList;
import java.util.List;

public class ChatAdapter extends BaseAdapter {

    private Context context;
    public static final String KEY = "key";
    public static final String VALUE = "value";

    public static final int VALUE_LEFT_TEXT = 1;//左边的简单文本item
    public static final int VALUE_RIGHT_TEXT = 2;//右边的简单文本item
    public static final int VALUE_LEFT_TEXT_SPANER = 3;//支持高亮显示的单一纯文本item
    public static final int VALUE_LEFT_SHOP_RECYCLEVIEW = 4;//横向的recyclerview
    public static final int VALUE_LEFT_FIRST_LOGIN = 5;
    public static final int VALUE_LEFT_TUANGOU_RECYCLEVIEW=6;
    public static final int VALUE_LEFT_RECOMMENDFOOD_RECYCLEVIEW=7;
    public static final int VALUE_LEFT_TRAVEL_SHOP_RANK_RECYCLEVIEW=8;
    public static final int VALUE_LEFT_ORDER_DETAILS = 9;//订座item
    public static final int VALUE_LEFT_DEQUEUE_DETAILS = 10;//排号item
    public static final int VALUE_LEFT_GET_DEQUEUEID = 11;//获取是否可以网上排号item
    public static final int VALUE_LEFT_TWO_ROWS_TEXT = 12;//普通两个不textview item
    private LayoutInflater mInflater;
    private List<Message> myList;
    private LinearLayoutManager mLayoutManager;

    public ChatAdapter(Context context, List<Message> myList) {
        this.context = context;
        this.myList = myList;

        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return myList==null?0:myList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return myList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {

        Message msg = myList.get(position);
        int type = getItemViewType(position);
        ViewHolderRightText holderRightText = null;
        ViewHolderLeftText holderLeftText = null;
        ViewHolderLeftLogin holderLeftLogin = null;
        if (convertView == null) {
            switch (type) {
                // 左边
                case VALUE_LEFT_TEXT:
                    holderLeftText = new ViewHolderLeftText();
                    convertView = mInflater.inflate(R.layout.item_chat_received_message,
                            null);
                    holderLeftText.ivLeftIcon = (ImageView) convertView
                            .findViewById(R.id.iv_avatar);
                    holderLeftText.btnLeftText = (TextView) convertView
                            .findViewById(R.id.tv_message);
                    holderLeftText.btnLeftText.setText(msg.getValue());
                    convertView.setTag(holderLeftText);
                    break;
                //右边消息,右边全是文本
                case VALUE_RIGHT_TEXT:
                    holderRightText = new ViewHolderRightText();
                    convertView = mInflater.inflate(R.layout.item_chat_sent_message,
                            null);
                    holderRightText.ivRightIcon = (ImageView) convertView
                            .findViewById(R.id.iv_avatar);
                    holderRightText.btnRightText = (TextView) convertView
                            .findViewById(R.id.tv_message);
                    holderRightText.btnRightText.setText(msg.getValue());
                    convertView.setTag(holderRightText);
                    break;
                case VALUE_LEFT_TEXT_SPANER:
                    holderLeftText = new ViewHolderLeftText();
                    convertView = mInflater.inflate(R.layout.item_chat_received_message_spanner,
                            null);
                    holderLeftText.ivLeftIcon = (ImageView) convertView
                            .findViewById(R.id.iv_avatar);
                    holderLeftText.spannableTextView = (SpannableTextView) convertView
                            .findViewById(R.id.tv_message);
                    holderLeftText.spannableTextView.setSpecifiedTextsColor(msg.getValue(), msg.getSpanerText(), R.color.red);
                    convertView.setTag(holderLeftText);
                    break;
                case VALUE_LEFT_FIRST_LOGIN:
                    holderLeftLogin = new ViewHolderLeftLogin();
                    convertView = mInflater.inflate(R.layout.item_first_chat_receive, null);
                    holderLeftLogin.ivLeftIcon = (ImageView) convertView
                            .findViewById(R.id.iv_avatar);
                    holderLeftLogin.tvLeft = (TextView) convertView.findViewById(R.id.tv_message);
                    holderLeftLogin.tvLeft.setText(msg.getValue());
                    holderLeftLogin.tvRes = (TextView) convertView.findViewById(R.id.tv_chose_res);
                    holderLeftLogin.tvRes.setText(msg.getLeftTag());
                    holderLeftLogin.tvTakeAway = (TextView) convertView.findViewById(R.id.tv_chose_takeaway);
                    holderLeftLogin.tvTakeAway.setText(msg.getRightTag());
                    convertView.setTag(holderLeftLogin);
                    break;

                case VALUE_LEFT_SHOP_RECYCLEVIEW:
                    holderLeftText = new ViewHolderLeftText();
                    convertView = mInflater.inflate(R.layout.item_chat_received_message_shop,
                            null);
                    holderLeftText.recyclerView = (RecyclerView) convertView.findViewById(R.id.rv_item_mesage_shop);
                    mLayoutManager = new LinearLayoutManager(context);
                    mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    holderLeftText.recyclerView.setLayoutManager(mLayoutManager);
                    View view = LayoutInflater.from(context).inflate(R
                            .layout.recyclerview_change_, null, false);
                    ShopAdapter adapter = new ShopAdapter(context, getShopList());
                    adapter.addFooterView(view);
                    holderLeftText.recyclerView.setAdapter(adapter);
                    convertView.setTag(holderLeftText);
                    break;

                case VALUE_LEFT_TRAVEL_SHOP_RANK_RECYCLEVIEW:
                    holderLeftText = new ViewHolderLeftText();
                    convertView = mInflater.inflate(R.layout.item_chat_received_message_shop,
                            null);
                    holderLeftText.recyclerView = (RecyclerView) convertView.findViewById(R.id.rv_item_mesage_shop);
                    mLayoutManager = new LinearLayoutManager(context);
                    mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    holderLeftText.recyclerView.setLayoutManager(mLayoutManager);
                    View view3 = LayoutInflater.from(context).inflate(R
                            .layout.recyclerview_change_, null, false);
                    ShopAdapter adapter3 = new ShopAdapter(context, getShopList());
                    adapter3.addFooterView(view3);
                    holderLeftText.recyclerView.setAdapter(adapter3);
                    convertView.setTag(holderLeftText);
                    break;

                case VALUE_LEFT_TUANGOU_RECYCLEVIEW:
                    holderLeftText=new ViewHolderLeftText();
                    convertView=mInflater.inflate(R.layout.item_chat_received_message_shop,null);
                    holderLeftText.recyclerView=(RecyclerView)convertView.findViewById(R.id.rv_item_mesage_shop);
                    mLayoutManager=new LinearLayoutManager(context);
                    mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    holderLeftText.recyclerView.setLayoutManager(mLayoutManager);
                    TuangouDetailAdapter adapter1=new TuangouDetailAdapter(context,getTuangouDetailList());
                    holderLeftText.recyclerView.setAdapter(adapter1);
                    convertView.setTag(holderLeftText);
                    break;
                    
                case VALUE_LEFT_ORDER_DETAILS:
                    holderLeftText = new ViewHolderLeftText();
                    convertView = mInflater.inflate(R.layout.item_chat_received_order,
                            null);
                    convertView.setTag(holderLeftText);
                    break;

                case VALUE_LEFT_DEQUEUE_DETAILS:
                    holderLeftText = new ViewHolderLeftText();
                    convertView = mInflater.inflate(R.layout.item_chat_received_dequeue,
                            null);
                    convertView.setTag(holderLeftText);
                    break;

                case VALUE_LEFT_GET_DEQUEUEID:
                    holderLeftText = new ViewHolderLeftText();
                    convertView = mInflater.inflate(R.layout.item_chat_received_get_dqid,
                            null);
                    convertView.setTag(holderLeftText);
                    break;

                case VALUE_LEFT_TWO_ROWS_TEXT:
                    holderLeftText = new ViewHolderLeftText();
                    convertView = mInflater.inflate(R.layout.item_chat_received_message_twolines,
                            null);
                    convertView.setTag(holderLeftText);
                    break;

                case VALUE_LEFT_RECOMMENDFOOD_RECYCLEVIEW:
                    holderLeftText=new ViewHolderLeftText();
                    convertView=mInflater.inflate(R.layout.item_chat_received_message_shop,null);
                    holderLeftText.recyclerView=(RecyclerView)convertView.findViewById(R.id.rv_item_mesage_shop);
                    mLayoutManager=new LinearLayoutManager(context);
                    mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    holderLeftText.recyclerView.setLayoutManager(mLayoutManager);
                    RecommendFoodAdapter adapter2=new RecommendFoodAdapter(context,getRecommendList());
                    holderLeftText.recyclerView.setAdapter(adapter2);
                    convertView.setTag(holderLeftText);
                    break;

                default:
                    break;
            }


        }
        else {
            switch (type) {
                case VALUE_LEFT_TEXT:
                    holderLeftText = (ViewHolderLeftText) convertView.getTag();
                    holderLeftText.btnLeftText.setText(msg.getValue());
                    break;
                case VALUE_RIGHT_TEXT:
                    holderRightText = (ViewHolderRightText) convertView.getTag();
                    holderRightText.btnRightText.setText(msg.getValue());
                    break;
                case VALUE_LEFT_TUANGOU_RECYCLEVIEW:
                    holderLeftText = (ViewHolderLeftText) convertView.getTag();
                    mLayoutManager = new LinearLayoutManager(context);
                    mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    holderLeftText.recyclerView.setLayoutManager(mLayoutManager);
                    View view = LayoutInflater.from(context).inflate(R
                            .layout.recyclerview_change_, null, false);
                    TuangouDetailAdapter adapter=new TuangouDetailAdapter(context,getTuangouDetailList());
                    holderLeftText.recyclerView.setAdapter(adapter);
                    break;
                case VALUE_LEFT_TEXT_SPANER:
                    holderLeftText = (ViewHolderLeftText) convertView.getTag();
                    holderLeftText.spannableTextView.setSpecifiedTextsColor(msg.getValue(),msg.getSpanerText(),R.color.red);

                    break;

                case VALUE_LEFT_ORDER_DETAILS:
                    holderLeftText = (ViewHolderLeftText) convertView.getTag();

                    break;

                case VALUE_LEFT_DEQUEUE_DETAILS:
                    holderLeftText = (ViewHolderLeftText) convertView.getTag();
                    break;

                case VALUE_LEFT_GET_DEQUEUEID:
                    holderLeftText = (ViewHolderLeftText) convertView.getTag();
                    break;

                case VALUE_LEFT_TWO_ROWS_TEXT:
                    holderLeftText = (ViewHolderLeftText) convertView.getTag();
                    break;
//                case VALUE_LEFT_TEXT_SPANER:
//                    holderLeftText = (ViewHolderLeftText) convertView.getTag();
//                    holderLeftText.ivLeftIcon.setImageDrawable(msg.);
//                    holderLeftText.btnLeftText.setText(msg.getValue());
//                case VALUE_LEFT_FIRST_LOGIN:
//                     holderLeftLogin=(ViewHolderLeftLogin)convertView.getTag();
//                     holderLeftLogin.ivLeftIcon.setImageDrawable(msg.);
//                     holderLeftLogin.tvLeft.setText();
//                     holderLeftLogin.tvTakeAway.setText();


                default:
                    break;
            }

            //holder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }


    @Override
    public int getItemViewType(int position) {

        Message msg = myList.get(position);
        int type = msg.getType();
        Log.e("TYPE:", "" + type);
        return type;
    }

    @Override
    public int getViewTypeCount() {
        return 20;
    }

    class ViewHolderRightText {
        private ImageView ivRightIcon;
        private TextView btnRightText;
    }


    class ViewHolderLeftText {
        private ImageView ivLeftIcon;
        private SpannableTextView spannableTextView;
        private TextView btnLeftText;
        private TextView titleText;
        private TextView cotentText;
        private RecyclerView recyclerView;
    }

    class ViewHolderLeftLogin {
        private ImageView ivLeftIcon;
        private TextView tvLeft;
        private TextView tvRes;
        private TextView tvTakeAway;
    }



    private List<ShopBean> getShopList() {
        List<ShopBean> shoplist = new ArrayList<ShopBean>();
        ShopBean  ShopBean;
        for (int i = 0; i < 8; i++) {
            ShopBean = new ShopBean();
            if (i % 2 == 0) {
                ShopBean.setHaveTuangou(true);
            } else {
                ShopBean.setHaveTuangou(false);
            }
            ShopBean.setDpTitle("满汉全席" + i);
            ShopBean.setDpDistance("<" + ((i + 1) * (100 + i)) + "m");
            ShopBean.setDpPrice("$" + (i + 100) + "/人");
            shoplist.add(ShopBean);
        }
        return shoplist;
    }

    private List<TuangouDetailBean> getTuangouDetailList(){
        List<TuangouDetailBean>   tuangouDetailBeanList =new ArrayList<TuangouDetailBean>();
        TuangouDetailBean  TuangouDetailBean;
        for(int i=0;i<6;i++){
            TuangouDetailBean=new TuangouDetailBean();
            TuangouDetailBean.setTuanName(i+"人餐烛光晚宴");
            TuangouDetailBean.setTuanOriginPrice("$"+(99+i));
            TuangouDetailBean.setTuanDicoutPrice("$"+(66+i));
            TuangouDetailBean.setTuanNumberSell("已售"+(200+i));
            tuangouDetailBeanList.add(TuangouDetailBean);

        }
        return tuangouDetailBeanList;
    }

    private  List<RecommendFoodBean>  getRecommendList(){
        List<RecommendFoodBean> recommendFoodBeanList=new ArrayList<RecommendFoodBean>();
        RecommendFoodBean RecommendFoodBean;
        for(int i=0;i<6;i++){
            RecommendFoodBean=new RecommendFoodBean();
            RecommendFoodBean.setRecommendName("青岛龙虾58一只");
            RecommendFoodBean.setRecommendNumber("top"+i);
            RecommendFoodBean.setRecommendSum("总共有"+i+"家店");
            recommendFoodBeanList.add(RecommendFoodBean);
        }

        return  recommendFoodBeanList;
    }



}
